package com.ykdl.member.views;

import com.ykdl.member.base.QLog;

import android.view.GestureDetector;
import android.view.MotionEvent;
/**
 * 
 * 巧妈记账的数据bean
 *
 * @since 1.0
 */
public class DrawArcControl
{
	private DrawArcView mDrawArcView;
	private DrawArcData mDrawArcData;
	private GestureDetector gestureDetector;
	private OnClickInterval clickInterval;
	private float offset;

	public DrawArcControl(DrawArcView drawArcView) 
	{
		this.mDrawArcView = drawArcView;
		this.mDrawArcData = mDrawArcView.getDrawArcData();
		gestureDetector = new GestureDetector(new MyGestureListener());
	}

	public void setTouch(MotionEvent event)
	{
		gestureDetector.onTouchEvent(event);
	}

	public void setUdata(float[] data)
	{
		for (float f : data)
		{
			QLog.info("data: " + f);
		}

		float total = 0;
		float[] start = mDrawArcData.getStartArc();
		float[] sweep = mDrawArcData.getSweepArc();

		for (int i = 0; i < data.length; i++)
		{
			total += data[i];
		}

		for (int i = 0; i < sweep.length; i++)
		{
			if (total != 0)
			{
				sweep[i] = (data[i] / total) * 360;
				float start_f = offset;
				for (int j = i - 1; j > -1; j--)
				{
					start_f += sweep[j];
				}
				start[i] = start_f;
			} else
			{
				sweep[i] = 0;
				start[i] = 0;
			}
		}
		mDrawArcData.setmNumTotal("￥" + total);
		mDrawArcView.setUpdate();
		mDrawArcData.countPoint(start,sweep);
	}

	public interface OnClickInterval
	{
		public void clickInterval(int data);
	}

	public void setOnClickInterval(OnClickInterval clickInterval)
	{
		this.clickInterval = clickInterval;
	}

	class MyGestureListener implements GestureDetector.OnGestureListener
	{
		private float X;
		private float Y;

		/**
		 * 根据点击的坐标算出区间 [0-90),[90-180),[180-270),[270-360)
		 * 
		 * @param X
		 * @param Y
		 * @return 0 , 1, 2, 3
		 */
		public int getIntervalByXY()
		{
			int result = -1;
			float centralX = mDrawArcData.getmCentralX();
			float centralY = mDrawArcData.getmCentralY();

			if (X > centralX && Y >= centralY)
			{
				result = 0;
			} else if (X <= centralX && Y > centralY)
			{
				result = 1;
			} else if (X < centralX && Y <= centralY)
			{
				result = 2;
			} else if (X >= centralX && Y < centralY)
			{
				result = 3;
			}
			return result;

		}

		// 得到点击位的角度，用以计算扇形区间
		public float getAngle(int interval)
		{
			float result = 0;

			float centralX = mDrawArcData.getmCentralX();
			float centralY = mDrawArcData.getmCentralY();

			float subtense = 0; // 对边
			float adjacent = 0; // 邻边
			int addAngle = 0; // 增加角度
			switch (interval)
			{
			case 0:
			{
				subtense = Y - centralY;
				adjacent = X - centralX;
				// addAngle = 0;
			}
				break;
			case 1:
			{
				subtense = centralX - X;
				adjacent = Y - centralY;
				addAngle = 90;
			}
				break;
			case 2:
			{
				subtense = centralY - Y;
				adjacent = centralX - X;
				addAngle = 180;
			}
				break;
			case 3:
			{
				subtense = X - centralX;
				adjacent = centralY - Y;
				addAngle = 270;
			}
				break;
			}

			if (adjacent != 0)
			{
				double arc = subtense / adjacent;
				result = (float) (Math.atan(arc) * 180 / Math.PI);
			}
			result += addAngle;
			return result;

		}

		@Override
		public boolean onDown(MotionEvent e)
		{
			// TODO Auto-generated method stub
			X = e.getX();
			Y = e.getY();

			if (clickInterval != null)
			{
				float[] start = mDrawArcData.getOldStart();
				int interval = getIntervalByXY();
				float angle = getAngle(interval);
				int result = -1;
				for (int i = 1; i < start.length; i++)
				{
					if (start[i] > angle)
					{
						result = i - 1;
						break;
					}
				}
				if (result == -1)
				{
					result = mDrawArcData.getArc_size() - 1;
				}
				clickInterval.clickInterval(result);
			}
			return true;
		}

		@Override
		public void onShowPress(MotionEvent e)
		{
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onSingleTapUp(MotionEvent e)
		{
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY)
		{
			return true;
		}

		@Override
		public void onLongPress(MotionEvent e)
		{
			// TODO Auto-generated method stub

		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY)
		{
			return true;
		}

	}
}
